package com.dzx;

import com.kumkee.userAgent.UserAgent;
import com.kumkee.userAgent.UserAgentParser;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年12月05日 12:19:06
 */
public class UserAgentTest {




      @Test
      public  void  testReadFile() throws  Exception{

          String path = "D:\\new-dev-project\\hadooptrain\\src\\main\\java\\com\\dzx\\imooc.log";
          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

          String line ;
          UserAgentParser userAgentParser = new UserAgentParser();
          Map<String,Integer> resultMap = new HashMap<String, Integer>();
          while ((line= bufferedReader.readLine())!=null) {

              if(StringUtils.isNotBlank(line)){
                String  source =    line.substring(getCharacterPosition(line, "\"",7)+1);
               // System.out.println(source);
                  UserAgent agent = userAgentParser.parse(source);
                  String browser = agent.getBrowser();
                  String platform = agent.getPlatform();
                  System.out.println(platform);

                  Integer value = resultMap.get(platform);
                  if(value==null){
                      resultMap.put(platform,1);
                  }else{
                      resultMap.put(platform,value+1);
                  }
              }
          }

            for (Map.Entry<String,Integer> entry:resultMap.entrySet()){
                System.out.println(entry.getKey()+":"+entry.getValue());
            }

      }

      @Test
    public  void  testGetCharacterPosition(){
          String log = "183.162.52.7 - - [10/Nov/2016:00:01:02 +0800] \"POST /api3/getadv HTTP/1.1\" 200 813 \"www.imooc.com\" \"-\" cid=0&timest\n" +
                  "amp=1478707261865&uid=2871142&marking=androidbanner&secrect=a6e8e14701ffe9f6063934780d9e2e6d&token=f51e97d1cb1a9caa\n" +
                  "c669ea8acc162b96 \"mukewang/5.0.0 (Android 5.1.1; Xiaomi Redmi 3 Build/LMY47V),Network 2G/3G\" \"-\" 10.100.134.244:80 \n" +
                  "200 0.027 0.027";
          int  index = getCharacterPosition(log,"\"",7);
          System.out.println(index);
      }

    /**
     * 获取指定字符串中指定标识的字符串出现的索引位置
     * @param value
     * @param operator
     * @param index
     * @return
     */
       private  int getCharacterPosition(String  value,String operator,int index){
           Matcher matcher = Pattern.compile(operator).matcher(value);
           int  mIdx = 0;
           while (matcher.find()){
                mIdx ++;
               if(mIdx == index){
                    break;
               }
           }
           return  matcher.start();
      }


      @Test
      public  void testUserAgentParser() {
          String source = "mukewang/5.0.0 (Android 5.1.1; Xiaomi Redmi 3 Build/LMY47V),Network 2G/3G";
          UserAgentParser userAgentParser = new UserAgentParser();
          UserAgent agent = userAgentParser.parse(source);
          String browser = agent.getBrowser();
          System.out.println(browser);
          System.out.println(agent.getPlatform());
          System.out.println(agent.getEngine());
          System.out.println(agent.getEngineVersion());
          System.out.println(agent.getOs());
          System.out.println(agent.getVersion());
          System.out.println(agent.isMobile());
      }
}
